// lucky.js
const util = require("../../utils/util")
Page({
    data: {
        end: ["00", "00", "00"]
    },
    onLoad: function (options) {
        this.setData(options)
    },
    onShow() {
        this.loadData()
    },

    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.get("lucky/detail", {
            data: { id: this.data.id }
        }).then(res => {
            res.data.lucky.team_num = +res.data.lucky.team_num
            res.data.lucky.end_time = util.formatTime(new Date(res.data.lucky.end_at * 1000))
            const mid = wx.getStorageSync("mid")
            res.data.isJoin = res.data.lucky.goodsOrder.findIndex(o => o.member_id == mid) >= 0
            this.setData(res.data)
            wx.stopPullDownRefresh()
            wx.hideLoading()
            this.startTimer()
        }).catch(err => {
            getApp().wxToast.error(err.message)
            wx.stopPullDownRefresh()
        })
    },

    startTimer() {
        clearInterval(this.tid)
        let t = this.data.lucky.end_at - parseInt(+new Date() / 1000)
        if (t <= 0) {
            return
        }
        function f() {
            t = this.data.lucky.end_at - parseInt(+new Date() / 1000)
            const needNum = +this.data.lucky.team_num
            const currentNum = this.data.lucky.goodsOrder.length
            if (t <= 0) {
                clearInterval(this.tid)
                if (needNum != currentNum) {
                    //人数不足
                    this.setData({
                        'lucky.status': -1
                    })
                } else {
                    this.setData({
                        'lucky.status': 2
                    })
                }
            }
            if (needNum == currentNum) {
                //人满
                this.setData({
                    'lucky.status': 2
                })
                clearInterval(this.tid)
                return
            }
            this.setData({
                end2: util.getTime(t).map(o => {
                    return {
                        t: o < 10 ? ('0' + o) : o,
                        raw: o
                    }
                })
            })
        }
        this.tid = setInterval(o => {
            f.call(this)
        }, 1000)
        f.call(this)
    },
    onPullDownRefresh() {
        this.loadData()
    },
    viewGoods() {
        if (getCurrentPages().length == 1) {
            wx.navigateTo({
                url: './detail/detail?id=' + this.data.lucky.goods.id,
            })
        } else {
            const ps = getCurrentPages()
            if (ps[ps.length - 2].route == "pages/goods/detail/detail") {
                wx.navigateBack({
                })
            } else {
                wx.navigateTo({
                    url: './detail/detail?id=' + this.data.lucky.goods.id,
                })
            }

        }
    },
    onShareAppMessage() {
        return {
            title: "【￥0.01】" + this.data.lucky.goods.name,
        }
    },
    submit() {
        wx.navigateTo({
            url: '/pages/pay/pay?' + ['lucky=1', "id=" + this.data.lucky.goods.id, "tid2=" + this.data.lucky.goods_order_id].join("&"),
        })
    },

    viewLuck() {
        wx.showLoading({
            title: '加载中...',
        })
        getApp().http.get("lucky/won", {
            data: {
                id: this.data.id
            }
        }).then(res => {
            this.setData({
                luckyList: res.data
            })
            wx.hideLoading()
        }).catch(err => {
            getApp().wxToast.error(err.message)
        })
    }
})